﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.IO;

namespace Tum.CollabXT.TFS
{
    /// <summary>
    /// Interaction logic for WssUploadItemData.xaml
    /// </summary>
    public partial class WssUploadItemData : System.Windows.Controls.UserControl
    {
        /// <summary>
        /// Gets or sets the header text displayed for this control.
        /// </summary>
        public string Header
        {
            get
            {
                return GroupBoxFrame.Header as string;
            }
            set
            {
                GroupBoxFrame.Header = value;
            }
        }

        /// <summary>
        /// Gets or sets the display mode for this control.
        /// </summary>
        public UploadType DisplayMode
        {
            get
            {
                return _DisplayMode;
            }
            set
            {
                _DisplayMode = value;

                //Modify control visibility accordingly
                if (_DisplayMode == UploadType.ProcessGuidance)
                {
                    NameGrid.Visibility = Visibility.Collapsed;
                    DefaultPageGrid.Visibility = Visibility.Visible;
                }
                if (_DisplayMode == UploadType.AdditionalLibrary)
                {
                    NameGrid.Visibility = Visibility.Visible;
                    DefaultPageGrid.Visibility = Visibility.Collapsed;
                }
            }
        }
        UploadType _DisplayMode;

        /// <summary>
        /// Gets or sets the name of the uploaded library. Only valid if Type=AdditionalLibrary.
        /// </summary>
        public string LibraryName
        {
            get
            {
                return TextBoxLibraryNameValue.Text;
            }
            set
            {
                TextBoxLibraryNameValue.Text = value;
            }
        }

        /// <summary>
        /// Gets or sets the source path of the uploaded library.
        /// </summary>
        public string LibraryDataPath
        {
            get
            {
                return LabelLibraryValue.Content as string;
            }
            set
            {
                if (!Directory.Exists(value))
                    throw new DirectoryNotFoundException();
                LabelLibraryValue.Content = value.Replace('/', '\\');
            }
        }

        /// <summary>
        /// Gets or sets the default page for the uploaded library. Only valid if Type=ProcessGuidance.
        /// </summary>
        public string DefaultLibraryPage
        {
            get
            {
                return LabelDefaultPageValue.Content as string;
            }
            set
            {
                if (value == null)
                {
                    LabelDefaultPageValue.Content = string.Empty;
                    return;
                }

                //Check if specified file is part of the document library
                var libraryDirInfo = new DirectoryInfo(LibraryDataPath);
                var selPageInfo = new FileInfo(value);
                if (!selPageInfo.Exists)
                {
                    throw new FileNotFoundException();
                }

                if (!selPageInfo.DirectoryName.Substring(0, libraryDirInfo.FullName.Length).Equals(libraryDirInfo.FullName, StringComparison.InvariantCulture))
                {
                    throw new TFSException("Default page not inside library.");
                }

                //Cut specified path and save
                LabelDefaultPageValue.Content = selPageInfo.FullName.Substring(libraryDirInfo.FullName.Length + 1).Replace('/', '\\');
            }
        }

        /// <summary>
        /// Event that is fired when the user clicks the remove library icon and wants to remove this library.
        /// </summary>
        public event EventHandler Remove;

        /// <summary>
        /// Event that is fired when the user modifies the control's data.
        /// </summary>
        public event EventHandler Modified;


        /// <summary>
        /// Constructor.
        /// </summary>
        public WssUploadItemData()
        {
            InitializeComponent();

            DisplayMode = UploadType.AdditionalLibrary;
        }

        private void ButtonSetLibraryPath_Click(object sender, RoutedEventArgs e)
        {
            using (FolderBrowserDialog fbd = new FolderBrowserDialog())
            {
                if (Directory.Exists(LibraryDataPath))
                    fbd.SelectedPath = LibraryDataPath;

                if (fbd.ShowDialog() == DialogResult.OK)
                {
                    if (!fbd.SelectedPath.Equals(LibraryDataPath))
                    {
                        DefaultLibraryPage = null;
                        LibraryDataPath = fbd.SelectedPath;

                        //Signal the control's data modification
                        if (Modified != null)
                            Modified.Invoke(this, e);
                    }
                }
            }
        }

        private void ButtonSetDefaultPage_Click(object sender, RoutedEventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Multiselect = false;

                //Check if library path has been set. Show warning if not

                if (Directory.Exists(LibraryDataPath))
                {
                    ofd.InitialDirectory = LibraryDataPath;
                }
                else
                {
                    System.Windows.MessageBox.Show(Tum.CollabXT.TFS.Resources.Language.ConfigPage_WarningSetLibraryPathFirst, Tum.CollabXT.TFS.Resources.Language.ConfigPage_WarningTitle, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }


                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        if (!ofd.FileName.Equals(DefaultLibraryPage))
                        {
                            DefaultLibraryPage = ofd.FileName;

                            //Signal the control's data modification
                            if (Modified != null)
                                Modified.Invoke(this, e);
                        }
                    }
                    catch
                    {
                        //Could not be set
                        System.Windows.MessageBox.Show(Tum.CollabXT.TFS.Resources.Language.ConfigPage_WarningDefaultNotPartOfLibrary, Tum.CollabXT.TFS.Resources.Language.ConfigPage_WarningTitle, MessageBoxButton.OK, MessageBoxImage.Error);

                        //Open dialog again
                        ButtonSetDefaultPage_Click(sender, e);
                        return;
                    }
                }
            }
        }

        private void ButtonRemoveLibrary_Click(object sender, RoutedEventArgs e)
        {
            //Signal that this library has to be removed
            if (Remove != null)
                Remove.Invoke(this, e);
        }

        private void TextBoxLibraryNameValue_TextChanged(object sender, TextChangedEventArgs e)
        {
            //Signal the control's data modification
            if (Modified != null)
                Modified.Invoke(this, e);
        }
    }

    /// <summary>
    /// Display type for the WssUploadItemData control.
    /// </summary>
    public enum UploadType
    {
        ProcessGuidance,
        AdditionalLibrary
    }
}
